package cn.xfdzx.android.apps.shop.net;

import org.reactivestreams.Publisher;

import io.reactivex.Flowable;
import io.reactivex.FlowableTransformer;
import io.reactivex.Observable;
import io.reactivex.ObservableSource;
import io.reactivex.ObservableTransformer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.annotations.NonNull;
import io.reactivex.schedulers.Schedulers;

/**
 * @author azheng
 * @date 2018/4/16.
 * description：RXjava 线程调度
 */
public class RxScheduler {

  /**
   * 统一线程处理
   * Flowable 解决了背压问题，在性能上低于Observable
   * @param <T> 指定的泛型类型
   * @return FlowableTransformer
   */
  public static <T> FlowableTransformer<T, T> Flo_io_main() {
    return new FlowableTransformer<T, T>() {
      @Override
      public Publisher<T> apply(@NonNull Flowable<T> upstream) {
        return upstream.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread());
      }
    };
  }

  /**
   * 统一线程处理
   *
   * @param <T> 指定的泛型类型
   * @return ObservableTransformer
   */
  public static <T> ObservableTransformer<T, T> Obs_io_main() {
    return new ObservableTransformer<T, T>() {
      @Override
      public ObservableSource<T> apply(Observable<T> upstream) {
        return upstream.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread());
      }
    };
  }

}
